package com.cwd.arm.application.service.read;

import com.cwd.arm.application.dto.TargetConfirmAmountDto;
import com.cwd.arm.application.service.read.facade.TargetConfirmAmountReadService;
import com.cwd.arm.infrastructure.dao.TargetConfirmAmountDao;
import com.cwd.arm.infrastructure.po.TargetConfirmAmountPo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* Description:目标确认金额
* Created on 2022年01月15日
* @author  yan
*/
@Repository
@Primary
public class TargetConfirmAmountReadServiceImpl implements TargetConfirmAmountReadService {

    @Autowired
    private TargetConfirmAmountDao targetConfirmAmountDao;

    @Override
    public List<TargetConfirmAmountDto> selectByProjectId(Long projectId) {
        List<TargetConfirmAmountPo> poList = targetConfirmAmountDao.selectByProjectId(projectId);
        poList = poList.stream().sorted(Comparator.comparing(TargetConfirmAmountPo::getYear)).collect(Collectors.toList());
        return convertToDtoList(poList);
    }

    @Override
    public TargetConfirmAmountDto selectByProjectYear(Long projectId, int year) {
        return convertToDto(targetConfirmAmountDao.selectByProjectYear(projectId,year));
    }

    private List<TargetConfirmAmountDto> convertToDtoList(List<TargetConfirmAmountPo> poList){
        return poList.stream().map(po->(convertToDto(po))).collect(Collectors.toList());
    }

    private TargetConfirmAmountDto convertToDto(TargetConfirmAmountPo po){
        if(po == null){
            return null;
        }
        TargetConfirmAmountDto dto = new TargetConfirmAmountDto();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }

}
